package com.hbwxz.admin.controller;

import com.hbwxz.admin.pojo.CommodityCategory;
import com.hbwxz.admin.service.CommodityCategoryService;
import com.hbwxz.common.response.CommonResponse;
import com.hbwxz.common.response.ResponseCode;
import com.hbwxz.common.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/commodity/category")
public class CommodityCategoryController {

    @Autowired
    private CommodityCategoryService commodityCategoryService;

    //商品目录查询
    @RequestMapping("/list")
    public CommonResponse getCategoryList(){
        return ResponseUtils.okResponse(commodityCategoryService.getCategoryList());
    }

    @RequestMapping("/create")
    public CommonResponse createCategory(@RequestBody CommodityCategory commodityCategory){
        if(commodityCategory.getLevel()>3 || commodityCategory.getLevel()<0){
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(),null,"level should be 1,2,3");
        }
        try {
            commodityCategoryService.createCategory(commodityCategory);
            return ResponseUtils.okResponse(null);
        }catch (Exception e){
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(),null,e.getMessage());
        }
    }

    @RequestMapping("/delete/{id}")
    public CommonResponse deleteCategory(@PathVariable Long id){
//        commodityCategoryService.deleteCategory(id);
        return ResponseUtils.okResponse(null);
    }

    @RequestMapping("/createBrandRelation")
    public CommonResponse createBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                         @RequestParam(value = "categoryId") Long categoryId){
        commodityCategoryService.createBrandRelation(brandId,categoryId);
        return ResponseUtils.okResponse(null);
    }

    @RequestMapping("/deleteBrandRelation")
    public CommonResponse deleteBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                              @RequestParam(value = "categoryId") Long categoryId){
        commodityCategoryService.deleteBrandRelation(brandId,categoryId);
        return ResponseUtils.okResponse(null);
    }

}
